package com.culture.bootdemo.config;


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

/**
 * @author ：wen
 * @date ：Created in 2019/8/2 17:10
 * @description：: LettuceConnectionFactory
 */
@Configuration
public class RedisConfig {

//    SpringBoot提供的Redis存储序列化方式，常用的有以下几种：
//    JdkSerializationRedisSerializer：将数据序列化为对象
//    StringRedisSerializer：将数据序列化为字符串
//    Jackson2JsonRedisSerializer：将数据序列化为json
//    GenericJackson2JsonRedisSerializer

    @Bean(name = "springSessionDefaultRedisSerializer")
    public Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer() {
        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值（默认使用JDK的序列化方式:将数据序列化为对象）
        Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper om = new ObjectMapper();
        // 指定要序列化的域，field,get和set,以及修饰符范围，ANY是都有包括private和public
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 指定序列化输入的类型，类必须是非final修饰的，final修饰的类，比如String,Integer等会跑出异常
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(om);
        return serializer;
    }

    @Bean(name = "redisTemplate")
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(lettuceConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(jackson2JsonRedisSerializer());
        template.setHashValueSerializer(jackson2JsonRedisSerializer());
        //value序列化使用Jackson2JsonRedisSerializer的话，无法使用自增方法
        return template;
    }


    //配置redission 用于分布式锁：

//    /**
//     * 配置redisson
//     */
//    @Value("${spring.redis.host:}")
//    private String redisHost;
//
//    @Value("${spring.redis.port:}")
//    private String redisPort;
//
//    @Value("${spring.redis.database:}")
//    private Integer redisDatabase;
//
//    /**
//     * 注册Redission对象 单机版
//     *
//     * @return Redission客户端对象
//     */
//    @Bean
//    public RedissonClient redissonClient() {
//        Config config = new Config();
//        config.useSingleServer().setAddress("redis://" + redisHost + ":" + redisPort).setDatabase(redisDatabase);
//        return Redisson.create(config);
//    }
//
//    /**
//     * 注册Redission对象 集群版
//     *
//     * @return Redission客户端对象
//     */
//    @Bean
//    public RedissonClient redissonClient2() {
//        Config config = new Config();
//        config.useClusterServers()
//                .addNodeAddress("redis://" + redisHost + ":" + redisPort)
//                .addNodeAddress("redis://" + redisHost + ":" + redisPort)
//                .addNodeAddress("redis://" + redisHost + ":" + redisPort)
//                .addNodeAddress("redis://" + redisHost + ":" + redisPort);
//
//        return Redisson.create(config);
//    }
}
